Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for deterministic dependent samples in PyroSample [enhancement] #3376

Merged

Conversation

BenZickel
Copy link
Contributor

@BenZickel BenZickel commented Jun 23, 2024

Make pyro.nn.PyroSample(prior) statements sample deterministic values when the prior is a function that returns a torch.Tensor value, as described in the below example:

from pyro.nn import PyroSample, PyroModule
from pyro import distributions as dist

class Location(PyroModule):
    def __init__(self):
        super().__init__()
        # Independent priors
        self.radius = PyroSample(dist.LogNormal(0,1))
        self.theta = PyroSample(dist.Normal(0.5, 0.1))
        # Dependent deterministic
        self.true_x = PyroSample(lambda self: self.radius * self.theta.cos())
        self.true_y = PyroSample(lambda self: self.radius * self.theta.sin())
        # Dependent samples
        self.observed_x = PyroSample(lambda self: dist.Normal(self.true_x, 0.05))
        self.observed_y = PyroSample(lambda self: dist.Normal(self.true_y, 0.05))
        
    def forward(self):
        return self.true_x, self.true_y, self.observed_x, self.observed_y

In the above code the dependent deterministic samples can be converted to dependent samples by assigning them pyro.nn.PyroSample(prior) statements with the prior being a function returning a proper distribution, allowing simple exploration of various model complexity levels for the same problem.

The updated docs can be reviewed here.

@fehiepsi
Copy link
Member

Thanks, @BenZickel! The feature looks great.

@fehiepsi fehiepsi merged commit b55aa9d into pyro-ppl:dev Jun 27, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants